package com.cn.study.filter;

import com.alibaba.fastjson.JSONObject;
import com.cn.study.common.ResultCode;
import com.cn.study.entity.ResultVO;
import com.cn.study.service.GatwayRedisService;
import com.cn.study.tool.JwtTokeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * @author mjy(局部过滤器, 例如此过滤器只会针对web端平台校验)
 * @date 2021/12/29
 * @description
 */
@Component
public class WebTokenGatewayFilterFactory extends AbstractGatewayFilterFactory<WebTokenGatewayFilterFactory.Config> {


    private static final Logger log = LoggerFactory.getLogger(WebTokenGatewayFilterFactory.class);

    @Autowired
    private GatwayRedisService redisService;

    public WebTokenGatewayFilterFactory() {
        super(Config.class);
        log.info("Loaded GatewayFilterFactory [TokenAccessFilter....]");
    }

    @Override
    public GatewayFilter apply(Config config) {
        return (exchange, chain) -> {
            ServerHttpRequest request = exchange.getRequest();
            ServerHttpResponse response = exchange.getResponse();
            String path = request.getPath().value();
            if (config.anonResources.contains(path)) {//放开静态资源
                return chain.filter(exchange);
            }
            String token = request.getHeaders().getFirst(config.getAccessField());
            Long userId = JwtTokeUtil.getValue(token, "userId", Long.class);
            ResultVO resultVO = null;
            if (userId == null) {
                resultVO = ResultVO.fail(ResultCode.TOKEN_OVERDUE);
                String rest = JSONObject.toJSONString(resultVO);
                DataBuffer buffer = response.bufferFactory().wrap(rest.getBytes());
                return response.writeWith(Mono.just(buffer));
            }
            if (token.equals(redisService.getValue(String.valueOf(userId)))) {
                resultVO = ResultVO.fail(ResultCode.FAILED);
                String rest = JSONObject.toJSONString(resultVO);
                DataBuffer buffer = response.bufferFactory().wrap(rest.getBytes());
                return response.writeWith(Mono.just(buffer));
            }
            return chain.filter(exchange);
        };
    }

    //内部类配置类
    public static class Config {

        //装载字段名称
        private String accessField;

        //装载不需要兼备的路径
        private List<String> anonResources;

        public String getAccessField() {
            return accessField;
        }

        public void setAccessField(String accessField) {
            this.accessField = accessField;
        }

        public List<String> getAnonResources() {
            return anonResources;
        }

        public void setAnonResources(List<String> anonResources) {
            this.anonResources = anonResources;
        }
    }


}
